﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Text.RegularExpressions;
namespace CharpClassGen
{

    public   class CsharpClassGenerator
    {
        public List<string> Defines { get; } = new List<string>();
        public List<string> Attributes { get; } = new List<string>();
        public List<string> Define { get; } = new List<string>();
        public string className { get; set; }
        public string baseCalssName { get; set; }
        public string accesser { get; set; }
        public string classNameSpace { get; set; }
        public List<Field> Fields { get; } = new List<Field>();
        public List<Property> Propertys { get; } = new List<Property>();
        public List<string> describe = new List<string> ();
        public List<Method> Methods { get; } = new List<Method>();

        public bool isAbstruct;
        public bool isStatic;
        private string Abstract => isAbstruct ? "abstract" : "";
        private string Static => isStatic ? "static" : "";

        public void AddAttributes(string attribute)
        {
            Attributes.Add(attribute);
        }

        public CsharpClassGenerator(string className, string baseCalssName = "", string classNameSpace = "",Accesser accesser =  Accesser.Public):this
            ( className,  baseCalssName , classNameSpace , accesser.ToString().ToLower() )
        {
        }
            public CsharpClassGenerator(string className, string baseCalssName, string classNameSpace, string accesser   )
        {
            this.className = className;
            this.baseCalssName = baseCalssName;
            this.accesser = accesser;
            this.classNameSpace = classNameSpace;
        }
        private void SetMemberRetract(Member member) 
        {
            member.retrat = (classNameSpace == "") ? 1 : 2;
        }
        private string ClassRetract=> (classNameSpace == "") ? "" : "\t";
   

        public Field AddField(Field field) 
        {
            this.Fields.Add(field);
            SetMemberRetract(field);
            return field;
            
        }

        public Property AddProperty(Property property) 
        {
            this.Propertys.Add(property);
            SetMemberRetract(property);
            return property;
        }

        public Method AddMethod(Method method)
        {
            this.Methods.Add(method);
            SetMemberRetract(method);
            return method;
        }

        public void AddUserdNameSpace(string nameSpace)
        {
            this.Defines.Add(nameSpace);
        }

        public void AddDefine(string define)
        {
            this.Defines.Add(define);
        }

        public void AddDescribeOneLine(string desc) 
        {
            describe.Add(desc);
        }
      
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            //添加预编译指令
            foreach (var item in Define)
            {
                sb.AppendLine($"#define {item}");
            }
            //引用命名空间
            foreach (var item in Defines)
            {
                sb.AppendLine($"using {item};");
            }
            //命名空间开始-----------------------------------------------------------------------------------------------------------------------------
            sb.AppendLine();
            if (classNameSpace!="") sb.AppendLine($"namespace {classNameSpace}{{");

            
            //类注释
            foreach (var item in describe)
            {
                sb.AppendLine($"{ClassRetract}//{item}");
            }
            //类特性
            foreach (var item in Attributes)
            {
                sb.AppendLine($"{ClassRetract}[{item}]");
            }
            //类开始----------------------------------------------------------------------
            sb.Append($"{ClassRetract} {accesser} {Static} {Abstract} class {className}");
            if (baseCalssName!="")
            {
                sb.AppendLine($":{baseCalssName}{{");
            }
            else
            {
                sb.AppendLine("{");
            }
            //字段
            foreach (var item in Fields)
            {
                sb.AppendLine(item.ToString());
            }
            foreach (var item in Propertys)
            {
                sb.AppendLine(item.ToString());
            }
            //方法
            foreach (var item in Methods)
            {
                sb.AppendLine(item.ToString());
            }
            //类结束----------------------------------------------------------------------
            sb.AppendLine($"{ClassRetract}}}");
            //命名空间结束 -----------------------------------------------------------------------------------------------------------------------------
            if (classNameSpace != "")  sb.AppendLine("}");

            string classCodeStr = sb.ToString();
            return classCodeStr; //RemoveEmptyLines(classCodeStr);
        }

        string RemoveEmptyLines(string str) 
        {
            Regex reg = new Regex("^\\s*(?=\\r?$)\\n");
           return reg.Replace(str,"");
        }

        public  string Build()
        {
            //命名空间

            //类开始
            //字段
            //方法
            //类结束
            return "";
        }
    }
}



